package com.wmyg.antifake.service.impl;

import com.wmyg.antifake.entity.BkUser;
import com.wmyg.antifake.entity.UserAddress;
import com.wmyg.antifake.repository.BkUserRepository;
import com.wmyg.antifake.repository.UserAddressRepository;
import com.wmyg.antifake.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 王卓逸
 * @program TODO
 * @description TODO
 * @create 2019-05-26
 */
@Service
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    UserAddressRepository userAddressRepository;

    @Autowired
    BkUserRepository bkUserRepository;

    @Override
    public List<UserAddress> findUserAddressList(String username) {
        BkUser bkUser = bkUserRepository.findByUsername(username);
        List<UserAddress> defaultList = userAddressRepository.findByUserAndStateOrderByCreateTimeAsc(bkUser, 2);
        List<UserAddress> others = userAddressRepository.findByUserAndStateOrderByCreateTimeAsc(bkUser, 1);
        if (null != defaultList) {
            defaultList.addAll(others);
            return defaultList;
        } else {
            return others;
        }
    }

    @Override
    public Boolean insertUserAddress(UserAddress address, String username) {
        BkUser bkUser = bkUserRepository.findByUsername(username);
        if (null == bkUser) {
            return false;
        } else {
            address.setUser(bkUser);
            userAddressRepository.save(address);
            return true;
        }
    }

    @Override
    public Boolean updateUserAddress(UserAddress address, String username) {
        BkUser bkUser = bkUserRepository.findByUsername(username);
        if (null == bkUser) {
            return false;
        } else {
            UserAddress addressOld = userAddressRepository.findByAddressId(address.getAddressId());
            addressOld.setState(1);
            userAddressRepository.save(addressOld);
            address.setUser(bkUser);
            userAddressRepository.save(address);
            return true;
        }
    }

    @Override
    public Boolean setDefault(Integer addressId, String username) {
        BkUser bkUser = bkUserRepository.findByUsername(username);
        if (null == bkUser) {
            return false;
        } else {
            List<UserAddress> userAddresseList = userAddressRepository.findByUserAndStateGreaterThanOrderByCreateTimeAsc(bkUser, 0);
            for (UserAddress address :
                    userAddresseList) {
                if (address.getAddressId().equals(addressId)) {
                    address.setState(2);
                } else {
                    address.setState(1);
                }
                userAddressRepository.save(address);
            }
            return true;
        }
    }

    @Override
    public UserAddress getDefault(String username) {
        BkUser bkUser = bkUserRepository.findByUsername(username);
        if (null == bkUser) {
            return null;
        } else {
            List<UserAddress> userAddresseList = userAddressRepository.findByUserAndStateOrderByCreateTimeAsc(bkUser, 2);
            if (null == userAddresseList || userAddresseList.isEmpty()) {
                return null;
            } else {
                return userAddresseList.get(0);
            }
        }
    }

    @Override
    public Boolean setNotDefault(Integer addressId) {
        UserAddress userAddress = userAddressRepository.findByAddressId(addressId);
        userAddress.setState(1);
        return true;
    }
}
